<template>
    <el-drawer
        v-model="isShow"
        size="50%"
        :destroy-on-close="true"
        @close="closeHandle"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        :show-close="false"
    >
    <template #header="{ close, titleId, titleClass }">
            <h4 :id="titleId" :class="titleClass">{{ title }}</h4>
        </template>
    <el-form :model="form"  ref="formRef" :rules="rules" v-loading="loading">
        <el-row class="mb-4">
            <el-col :span="24">
                <el-form-item label="安检时间" required label-width="80px" prop="checkdate">
                    <el-date-picker
                    v-model="form.checkdate"
                    type="datetime"
                    placeholder="请选择安检时间"
                    value-format="YYYY-MM-DD HH:mm:ss"
                    style="width:100%"
                    :readonly="isreadonly"
                />
                </el-form-item>
            </el-col>
            <el-col :span="24">
                <el-form-item label="安检地点" label-width="80px">
                    <el-input v-model="form.checkplace" clearable :readonly="isreadonly"/>
                </el-form-item>
            </el-col>
            <el-col :span="24">
                <el-form-item label="安检方式" label-width="80px">
                    <el-input v-model="form.checkmethod" clearable  :readonly="isreadonly"/>
                </el-form-item>
            </el-col>
            <el-col :span="24">
                <el-form-item label="卡号" label-width="80px">
                    <el-input v-model="form.cardcode" clearable :readonly="isreadonly" />
                </el-form-item>
            </el-col>
            <el-col :span="24">
                <el-form-item label="安检情况" required label-width="80px" prop="checkdetail">
                    <el-input v-model="form.checkdetail" clearable :readonly="isreadonly" />
                </el-form-item>
            </el-col>
        </el-row>
    </el-form>
    <template #footer>
        <el-button type="primary" v-if="!isreadonly" @click="Confirm">确认</el-button>
        <el-button @click="isShow = false">关闭</el-button>
    </template>
    </el-drawer>
</template>
<script setup>

import {getCurrentInstance} from "vue";
const {proxy} = getCurrentInstance()
import {SecurityCheckAdd,SecurityCheckInfo,SecurityCheckEdit} from "@/api/ApplyDetail/ApplyDetail";
const {nj_sex,nj_sheanren_type,nj_sheanren_status} = proxy.useDict( 'nj_sex','nj_sheanren_type','nj_sheanren_status')

const isShow = ref(false)
const loading = ref(false)
const isreadonly = ref(false)

const title = ref('新增安检记录')
const form = ref({
})
const changeShowStatus = () => {
    isShow.value = !isShow.value
}
const rules = {
    checkdate: [{ required: true, message: "安检时间不能为空", trigger: "blur" }],
    checkdetail: [{ required: true, message: "安检情况不能为空", trigger: "blur" }],
}
// 通知事件
const emit = defineEmits(['getList'])
onMounted(() => {
    
})

// 获取详情
const getSecurityCheckInfo = async (jwApplydetailId) => {
    await SecurityCheckInfo(jwApplydetailId).then(res => {
      form.value = res.data
      loading.value = false
    })
}
const showSecurityCheckAdd = async (jwApplydetailId) => {
    changeShowStatus()
    console.log("show新增",jwApplydetailId)
    form.value.jwApplydetailId = jwApplydetailId
}

const showSecurityCheckEdit = async (data,isSee) => {
    changeShowStatus()
    if(isSee == 'See'){
        title.value = '安检信息详情'
        isreadonly.value = true
        loading.value = true
        getSecurityCheckInfo(data.jw_securityCheck_id)
        
    }else{
        title.value = '安检信息编辑'
        loading.value = true
        isreadonly.value = false
        getSecurityCheckInfo(data.jw_securityCheck_id)
    }
    
}
// 确认按钮
const Confirm = async () => {
    proxy.$refs["formRef"].validate(valid => {
    if (valid) {
        if(form.value['jwSecuritycheckId']){
            loading.value = true
            SecurityCheckEdit(form.value).then(res => {
                loading.value = false
                changeShowStatus()
                emit('getList', '来了老弟')
            })
        } else {
            SecurityCheckAdd(form.value).then(res => {
                loading.value = false
                changeShowStatus()
                proxy.$modal.msgSuccess(`新增成功`)
                emit('getList', '来了老弟')
            })
        }
    }
  });
}

defineExpose({showSecurityCheckAdd,showSecurityCheckEdit})

// 关闭后的回调事件
const closeHandle = () => {
    form.value = {}
    emit('getList', '来了来了')
    console.log(123123)
}
</script>
<style>

</style>